package org.krishna.xml;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.krishna.Dao.SaveData;
import org.krishna.jaxb.xml.Courses;
import org.krishna.jaxb.xml.Parent;
import org.krishna.jaxb.xml.Parents;
import org.krishna.jaxb.xml.Students;
import org.krishna.model.Login;
import org.krishna.model.Marks;
import org.krishna.model.Student;


public class GenerateXml {
	
	
	public static final String XML_START_TAG = "<?xml version=\"1.0\"?>";
	
	/** The Constant PARENTS_START_XML_TAG. */
	public static final String PARENTS_START_XML_TAG = "<parents \n" + "\"> \n";
	
	/** The Constant PARENTS_END_XML_TAG. */
	public static final String PARENTS_END_XML_TAG = "</parents>";
	
	public void buildParents(Parents parents, SaveData saveData){		
		List<Login> parentsData= saveData.getParents();
		for(Login parentData: parentsData){
			Parent parent = new Parent();			
			setParent(parent, parentData, saveData);
			parents.getParent().add(parent);			
		}
	}

	private void setParent(Parent parent, Login parentData, SaveData saveData) {		
		parent.setParentname(parentData.getUserName());
		parent.setParentemail(parentData.getEmail());
		List<org.krishna.jaxb.xml.Student> students= setStudents(parentData, saveData );
		parent.setStudents(students);			
	}
	
	private List<org.krishna.jaxb.xml.Student> setStudents(Login parentData, SaveData saveData) {
		List<org.krishna.jaxb.xml.Student> students = new ArrayList<org.krishna.jaxb.xml.Student>();
		Set<Student> studentDetails = parentData.getStudentDetails();
		for(Student studentDetail: studentDetails){			
			org.krishna.jaxb.xml.Student xmlStudent = new org.krishna.jaxb.xml.Student();
			xmlStudent.setId(String.valueOf(studentDetail.getStudentId()));
			xmlStudent.setName(studentDetail.getStudentName());
			List<org.krishna.jaxb.xml.Course> courses = setCourses(parentData, saveData);
			xmlStudent.setCourses(courses);
			students.add(xmlStudent);
	    }		
		return students;
	}
	
	private List<org.krishna.jaxb.xml.Course> setCourses(Login parentData, SaveData saveData){
    	List<org.krishna.jaxb.xml.Course> courses = new ArrayList<org.krishna.jaxb.xml.Course>();
    	Set<Marks> marksDetails = parentData.getMarksDetails();    	
    	for(Marks marks:marksDetails){
    		org.krishna.jaxb.xml.Course xmlCourse = new org.krishna.jaxb.xml.Course();
    		xmlCourse.setMarksobtained(marks.getMarksObtained());
    		xmlCourse.setMaxmarks(marks.getMaxMarks());
    		xmlCourse.setResult(marks.getResult());
    		xmlCourse.setName("ab");
    		courses.add(xmlCourse);
       	}
    	return courses;     	
    }
	
}
